<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/6/1
  Time: 9:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>下订单</title>
</head>
<style>
    .table1 tr:hover{
        background: honeydew;
    }
</style>
<script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
<script type="text/javascript">
    //alert(666);
    function  test1(obj){
        //alert(555);

        // var stocks = $(".limitCount").html();
        // // alert(stocks);
        // if (obj.value > stocks || this.value < 1 || !/^\d+$/.test(obj.value)) {
        //     obj.value = '';
        // }


        var amount=$("#amount").html();
        var price1=$(".price").eq(0).html();
        var stock1=$(".stocks").eq(0).val();

        var price2=$(".price").eq(1).html();
        var stock2=$(".stocks").eq(1).val();

        var price3=$(".price").eq(2).html();
        var stock3=$(".stocks").eq(2).val();

        var price4=$(".price").eq(3).html();
        var stock4=$(".stocks").eq(3).val();

        var price5=$(".price").eq(4).html();
        var stock5=$(".stocks").eq(4).val();

        // alert(price1+","+price2+","+price3+","+price4+","+price5);
        // alert(stock1+","+stock2+","+stock3+","+stock4+","+stock5);
        // alert(price1*stock1+price2*stock2+price3*stock3+price4*stock4+price5*stock5);
        if(stock1.length>0){
            $("#amount").html(price1*stock1);
        } if(stock1.length>0&&stock2.length>0){
            $("#amount").html(price1*stock1+price2*stock2);
        } if(stock1.length>0&&stock2.length>0&&stock3.length>0){
            $("#amount").html(price1*stock1+price2*stock2+price3*stock3);
        } if(stock1.length>0&&stock2.length>0&&stock3.length>0&&stock4.length>0){
            $("#amount").html(price1*stock1+price2*stock2+price3*stock3+price4*stock4);
        }  if(stock1.length>0&&stock2.length>0&&stock3.length>0&&stock4.length>0&&stock5.length>0){
            $("#amount").html(price1*stock1+price2*stock2+price3*stock3+price4*stock4+price5*stock5);
        }


    }
    Date.prototype.format = function(format) {
        /*
         * eg:format="YYYY-MM-dd hh:mm:ss";

         */
        var o = {
            "M+" :this.getMonth() + 1, // month
            "d+" :this.getDate(), // day
            "h+" :this.getHours(), // hour
            "m+" :this.getMinutes(), // minute
            "s+" :this.getSeconds(), // second
            "q+" :Math.floor((this.getMonth() + 3) / 3), // quarter
            "S" :this.getMilliseconds()
            // millisecond
        }
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
        }
        for ( var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                    : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }



    $(function () {
        // alert(444);
        //
        // var stocks = $(".limitCount").html();
        // alert(stocks);

        // var clear =function(obj) {
        //     var stocks = $(".limitCount").html();
        //     alert(stocks);
        //     if (obj.value > stocks || obj.value < 1 || !/^\d+$/.test(obj.value)) {
        //         obj.value = '';
        //     }
        // };
        // $(".stocks").onkeyup(clear(this));

        $.ajax({
            url:"ServletProvince",
            data:{pid:1},
            dataType:"json",
            type:"post",
            success:function (data,textStatus) {
                $.each(data,function (i,item) {
                    $("#pro").append("<option value='"+item.id+"'>"+item.district_name+"</option>");
                })
            },
            error:function (XMLHttpRequest,textStatus) {
                alert("请求失败，原因:"+XMLHttpRequest.status)
            }
        });
        $("#pro").change(function () {
            var pId=$(this).val();
            //alert(pId)
            $("#city option:gt(0)").remove();
            $("#county option:gt(0)").remove();
            if(pId>0){
                $.ajax({
                    url:"ServletProvince",
                    data:{pid:pId},
                    dataType:"json",
                    type:"post",
                    success:function (data,textStatus) {
                        $.each(data,function (i,item) {
                            $("#city").append("<option  value='"+item.id+"'>"+item.district_name+"</option>")
                        })
                    },
                    error:function (XMLHttpRequest,textStatus) {
                        alert("请求失败，原因："+XMLHttpRequest.status)
                    }
                });
            }
        });
        $("#city").change(function () {
            if($("#city").val()==0){};
            var cId=$(this).val();
            $("#county option:gt(0)").remove();
            if(cId>0){
                $.ajax({
                    url:"ServletProvince",
                    data:{pid:cId},
                    dataType:"json",
                    type:"post",
                    success:function (data,textStatus) {
                        $.each(data,function (i,item) {
                            $("#county").append("<option  value='"+item.id+"'>"+item.district_name+"</option>")
                        })
                    },
                    error:function (XMLHttpRequest,textStatus) {
                        alert("请求失败，原因："+XMLHttpRequest.status)
                    }
                });
            }
        });


        $("#sb1").click(function () {
            var did=$("#county").val();
            var amount=$("#amount").html();
            var uid=$("#ssid").html();
            var d = new Date();
            var orderNum=did+uid+amount+d.format("yyyy-MM-dd hh:mm:ss");

            var id1=$(".bookName").eq(0).html();
            var id2=$(".bookName").eq(1).html();
            var id3=$(".bookName").eq(2).html();
            var id4=$(".bookName").eq(3).html();
            var id5=$(".bookName").eq(4).html();
            var price1=$(".price").eq(0).html();
            var stock1=$(".stocks").eq(0).val();

            var price2=$(".price").eq(1).html();
            var stock2=$(".stocks").eq(1).val();

            var price3=$(".price").eq(2).html();
            var stock3=$(".stocks").eq(2).val();

            var price4=$(".price").eq(3).html();
            var stock4=$(".stocks").eq(3).val();

            var price5=$(".price").eq(4).html();
            var stock5=$(".stocks").eq(4).val();

           // alert(id1+id2+id3+id4+id5);
            alert("下单编号："+orderNum);
            if(did>0&&uid>0&&amount>0){
                $.post("ServletOrder",{did:did,amount:amount,uid:uid,orderNum:orderNum},function (data) {
                    //alert(data);
                    if(data=0){
                        alert("购物信息不全，请校验");
                        return false;
                    }else{
                       // alert(data);

                        // if(stock1.length>0){
                        //     $.post("ServletOrderDetails",{num:stock1,price:price1},function (data) {
                        //     });
                        // } if(stock1.length>0&&stock2.length>0){
                        //     $("#amount").html(price1*stock1+price2*stock2);
                        // } if(stock1.length>0&&stock2.length>0&&stock3.length>0){
                        //     $("#amount").html(price1*stock1+price2*stock2+price3*stock3);
                        // } if(stock1.length>0&&stock2.length>0&&stock3.length>0&&stock4.length>0){
                        //     $("#amount").html(price1*stock1+price2*stock2+price3*stock3+price4*stock4);
                        // }  if(stock1.length>0&&stock2.length>0&&stock3.length>0&&stock4.length>0&&stock5.length>0){
                        //     $("#amount").html(price1*stock1+price2*stock2+price3*stock3+price4*stock4+price5*stock5);
                        // }

                        window.location="ServletBook?method=findOrder&uid="+uid;

                        // alert("注册成功！这是你的账号："+un+"，密码："+pswd+"请重新登录吧。");
                        return true;
                    }
                });
            }



        });


        });

</script>
<style>
    .stocks{
        border:1px solid red ;
    }
</style>
<body>
<%--<form id="f1">--%>
    <%--<input  type="hidden"  value="setOrder"  name="method">--%>
<div style="position: absolute;margin-left: 75%;margin-top: 0%">
    <a href="ServletBook?method=find" ><img src="img/QQ浏览器截图20210531162717.png"></a>
</div>
    <table border="1" class="table1" style="width: 70%;text-align: center;border: none;border-collapse: collapse;">
        <tr>
            <th>编号</th>
            <th>书名</th>
            <th>单价</th>
            <th>详情</th>
            <th>库存量</th>
            <th>预览</th>
            <th>购买数量</th>
        </tr>
        <c:forEach  items="${lists}"  var="b"  varStatus="i">
            <tr>
                <td>
                        ${i.index+1}
                </td>
                <td style="display: none"  class="bookName">${b.id}</td>
                <td>${b.bookName}</td>
                <td class="price" id="price${i.index+1}">${b.price}</td>
                <td>${b.bookDescribe}</td>
                <td class="limitCount">${b.stock}</td>

                <td style="width: 40%;height:300px;">
                    <img src="${b.book_img}" style="width: 70%;height: 80%;padding: 10% 10%;">
                </td>

                <td style="width: 30%">
                    <c:if test="${b.stock==0}">
                        该书已售罄，不能购<br/>买啦！等待货源吧。
                    </c:if>
                    <c:if test="${b.stock>0}">

                        <input type="number" class="stocks" value="1"  oninput="if(value>${b.stock})value=${b.stock};if(value<0)value=1;test1(this);" ><br>
                        <span>输入的值上限已经被我<br>锁定为库存量了，(#^.^#)。</span>
                        <script>
                            //

                        </script>
                    </c:if>
                </td>
            </tr>
        </c:forEach>
    </table>

     <%--收货人姓名  等信息--%>
    选择收货地址:
    省份：
    <select id="pro">
        <option value="0">---请选择省份---</option>
    </select>
    城市：
    <select id="city">
        <option value="0">---请选择城市---</option>
    </select>
    区县：
    <select id="county">
        <option value="0">---请选择区县---</option>
    </select><br>
    <span id="ssid" style="display: none"><%=session.getAttribute("uid")%></span>

    <span id="amount" style="border: 1px solid lightgray">合计</span>


    <input  type="submit" id="sb1"  value="确认购买" >

<%--</form>--%>

<a href="ServletBook?method=find">返回商城，重新挑选</a>
<br>

<a href="ServletBook?method=find" ><img src="img/购物车推荐.png"></a>
</body>
</html>
